/*
 * Copyright 2016 Yan Zhenjie
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mingmen.canting.caigourenyuan;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mingmen.canting.R;
import com.mingmen.canting.activity.RegisterActivity;
import com.mingmen.canting.adapter.ShouhuoListAdapter;
import com.mingmen.canting.base.BaseBean;
import com.mingmen.canting.http.callback.StringCallback;
import com.mingmen.canting.httpconfig.HttpResponse;
import com.mingmen.canting.immersionbar.ImmersionBar;
import com.mingmen.canting.model.ShouHouModel;
import com.mingmen.canting.util.GsonUtil;
import com.mingmen.canting.util.SharePreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import okhttp3.Call;
import okhttp3.Response;


/**
 * <p>
 * ListView形式的侧滑菜单，支持横向和竖向的。
 * </p>
 * Created by Yan Zhenjie on 2016/7/27.
 */
public class ShouhuoListActivity extends Activity {

    @BindView(R.id.text_return)
    TextView textReturn;
    @BindView(R.id.linear_return)
    LinearLayout linearReturn;
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.img_right)
    ImageView imgRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.mToolbar)
    Toolbar mToolbar;
    @BindView(R.id.btn_add_address)
    Button btnRegister;
    private RecyclerView mRecyclerView;


    private ShouhuoListAdapter mAdapter;

    String supplierId;
    String token;

    List<ShouHouModel.DataBean> dataBeanList = new ArrayList<>();

    private int resurtResurant = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shouhuo);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        textCenter.setText("收货信息");
//        supplierId = getIntent().getStringExtra("supplierId");
        token = (String) SharePreferenceUtil.get(this, "token", "");
        ImmersionBar.with(this).titleBar(mToolbar)
                .navigationBarColor(R.color.black).init();
        mRecyclerView = (RecyclerView) findViewById(R.id.main_recycler);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ShouhuoListAdapter(this, dataBeanList);

        mRecyclerView.setAdapter(mAdapter);


        getData();

    }


    public void getData() {
        HttpResponse.getAddressList(token, new StringCallback(this) {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                Log.d("SHOUHUOLISt", s);
                if (dataBeanList.size() > 0) {
                    dataBeanList.clear();
                }
                ShouHouModel shouHouModel = new ShouHouModel();
                shouHouModel = GsonUtil.GsonToBean(s, ShouHouModel.class);
                dataBeanList.addAll(shouHouModel.getData());
                mAdapter.setDataList(dataBeanList);
            }
        });


        mAdapter.setOnitemClickLintener(new ShouhuoListAdapter.OnitemClick() {

            @Override
            public void onItemClick(View view, int position, List<ShouHouModel.DataBean> stringList) {
                Intent intent;
                switch (view.getId()) {
                    case R.id.img_modify:
//                        startActivity(new Intent(ShouhuoListActivity.this, ModifyAddressActivity.class));
                        intent = new Intent(ShouhuoListActivity.this, ModifyAddressActivity.class);
                        intent.putExtra("addressId", stringList.get(position).getAddressId());
                        intent.putExtra("addressName", stringList.get(position).getAddressName());
                        intent.putExtra("flag", stringList.get(position).getFlag());
                        intent.putExtra("linkman", stringList.get(position).getLinkman());
                        intent.putExtra("linkTel", stringList.get(position).getLinkTel());
                        intent.putExtra("houseNumber", stringList.get(position).getHouseNumber());
                        intent.putExtra("supplierId", supplierId);
                        intent.putExtra("gender", stringList.get(position).getGender());
                        startActivityForResult(intent, resurtResurant);
                        break;
                    default:

                        HttpResponse.updateAddress(token, stringList.get(position).getAddressId(), stringList.get(position).getAddressName(), 1 + "", stringList.get(position).getLinkman(), stringList.get(position).getLinkTel(), stringList.get(position).getHouseNumber(), supplierId, stringList.get(position).getGender(), new StringCallback(ShouhuoListActivity.this) {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {
                                Log.d("ADDADDREss", s);
                                BaseBean baseBean = new BaseBean();
                                baseBean = GsonUtil.GsonToBean(s, BaseBean.class);
                                if (baseBean.getCode() == 0) {
                                    Toast.makeText(ShouhuoListActivity.this,   "保存成功", Toast.LENGTH_SHORT).show();
                                    finish();

                                }

                            }
                        });

                        break;
                }
            }

        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == resurtResurant) {

            getData();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


    @OnClick({R.id.text_return, R.id.linear_return, R.id.btn_add_address})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.text_return:
                finish();
                break;
            case R.id.linear_return:
                finish();
                break;
            case R.id.btn_add_address:
//                startActivity(new Intent(ShouhuoListActivity.this, AddAddressActivity.class));
                intent = new Intent(ShouhuoListActivity.this, AddAddressActivity.class);
                intent.putExtra("supplierId", supplierId);
                startActivity(intent);

                break;
        }
    }

    public void onEventMainThread(Integer integer) {
        if (integer == 40) {
            getData();
        }

    }
}